package com.gopay.common.proccode.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.proccode.dao.IDicProcCodeDAO;

/**
 * 
 * @DicPrcoCodeDAOImpl.java
 * @author liujy
 * @2012-12-27 下午2:22:36 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("dicProcCodeDAO")
public class DicProcCodeDAOImpl extends BaseDAOImpl<Serializable, Serializable> implements IDicProcCodeDAO {

    private final String FIND_DIC_PROC_CODE_MER_GEN_BY_PROC_CODE_SQL = "select m.MER_PROC_CODE from DIC_PROC_CODE_MER_GEN m "
            + "where m.GOPAY_PROC_CODE=:procCode";

    private final String FIND_DIC_PROC_CODE_MER_BT_BY_PROC_CODE_SQL = "select b.MER_PROC_CODE from DIC_PROC_CODE_MER_BT b "
            + "where b.GOPAY_PROC_CODE=:procCode";

    private final String FIND_DIC_PROC_CODE_BY_PROC_CODE_SQL = "from DicProcCodeGopay where gopayProcCode=:procCode";

    @Override
    public List<?> getProcCodeGopayById(String procCode) {
        if (StringUtils.isEmpty(procCode)) {
            return null;
        }
        Map<String, Object> param = initParams(procCode);
        return findByHql(FIND_DIC_PROC_CODE_BY_PROC_CODE_SQL, param);
    }

    @Override
    public String getProcCodeMerGen(String procCode) {
        Map<String, Object> param = initParams(procCode);
        List<?> list = findBySql(FIND_DIC_PROC_CODE_MER_GEN_BY_PROC_CODE_SQL, param);
        if (list == null || list.size() != 1) {
            return null;
        }
        return (String) list.get(0);
    }

    @Override
    public String getProcCodeMerBT(String procCode) {
        Map<String, Object> param = initParams(procCode);
        List<?> list = findBySql(FIND_DIC_PROC_CODE_MER_BT_BY_PROC_CODE_SQL, param);
        if (list == null || list.size() != 1) {
            return null;
        }
        return (String) list.get(0);
    }

    private Map<String, Object> initParams(String procCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("procCode", procCode);
        return params;
    }
}
